package org.edu.lazymodel;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.edu.model.Student;
import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SelectableDataModel;
import org.primefaces.model.SortOrder;

public class StudentLazyDataModel extends LazyDataModel<Student> implements SelectableDataModel<Student> {

	private static final long serialVersionUID = 1L;
	
	private Student student;
	private List<Student> datasource;
	public StudentLazyDataModel(List<Student> datasource){
		this.datasource = datasource;
	}
	
	 @Override
	    public Student getRowData(String rowKey) {
	        for(Student student : datasource) {
	            if(student.getUserStudent().getUser_number().equals(rowKey))
	                return student;
	        }
	        return null;
	    }

	    @Override
	    public Object getRowKey(Student student) {
	        return student.getUserStudent().getUser_number();
	    }

	    @Override
	    public List<Student> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String,String> filters) {
	        List<Student> data = new ArrayList<Student>();
	        for(Student student: datasource) {
	            boolean match = true;
	            for(Iterator<String> it = filters.keySet().iterator(); it.hasNext();) {
	                try {
	                    String filterProperty = it.next();
	                    String filterValue = filters.get(filterProperty);
	                    String fieldValue = String.valueOf(student.getClass().getField(filterProperty).get(student));
	                    if(filterValue == null || fieldValue.startsWith(filterValue)) {
	                        match = true;
	                    }
	                    else {
	                        match = false;
	                        break;
	                    }
	                } catch(Exception e) {
	                    match = false;
	                } 
	            }

	            if(match) {
	                data.add(student);
	            }
	        }

	        //sort
//	        if(sortField != null) {
//	            Collections.sort(data, new LazySorter(sortField, sortOrder));
//	        }

	        int dataSize = data.size();
	        this.setRowCount(dataSize);

	        //paginate
	        if(dataSize > pageSize) {
	            try {
	                return data.subList(first, first + pageSize);
	            }
	            catch(IndexOutOfBoundsException e) {
	                return data.subList(first, first + (dataSize % pageSize));
	            }
	        }
	        else {
	            return data;
	        }
	    }

		public Student getStudent() {
			return student;
		}

		public void setStudent(Student student) {
			this.student = student;
		}

		public List<Student> getDatasource() {
			return datasource;
		}

		public void setDatasource(List<Student> datasource) {
			this.datasource = datasource;
		}
		
}
